package bat.iog.hytool.udp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class ServerChat {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(
                System.in));
        System.out.println("-----客服------");
        DatagramSocket ds = new DatagramSocket(8888);
        while(true) {
            /** 接收数据 */
            //准备接收数据
            byte[] buf = new byte[1024];
            //准备数据报进行接收
            DatagramPacket dp=new DatagramPacket(buf, buf.length);
            //接收
            ds.receive(dp);
            //查看接收到的数据
            String str = new String(buf, 0, dp.getLength());
            System.out.println("客户说：" + str);

            /** 发送数据 */
            byte[] buf2 = br.readLine().getBytes();
            DatagramPacket dp2 = new DatagramPacket(buf2,
                    buf2.length, dp.getAddress(), dp.getPort());
            ds.send(dp2);

            if(str.equals("bye")) {
                break;
            }
        }
        ds.close();
    }
}